/*
 * VentanaAgregarNodo.java
 *
 * Created on Sep 12, 2012, 7:50:30 PM
 */
package Interfaz;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author Emiliano
 */
public class VentanaAgregarNodo extends javax.swing.JFrame {
    private int filaSeleciona = -1;
    public VentanaPrincipal va;
    
    /** Creates new form VentanaAgregarNodo */
    public VentanaAgregarNodo(VentanaPrincipal va) {
        initComponents();
        this.va = va;
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelDatosNodo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtNombreNodo = new javax.swing.JTextField();
        txtNombreEtiqueta = new javax.swing.JTextField();
        txtValor = new javax.swing.JTextField();
        btnAgregarNodo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTElementos = new javax.swing.JTable();
        btnEliminarFila = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();

        setTitle("Agregar Nodo");

        jPanelDatosNodo.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Nodo"));

        jLabel1.setText("Nombre del Nodo:");

        jLabel2.setText("Nombre de la Etiqueta:");

        jLabel3.setText("Valor:");

        txtNombreNodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreNodoActionPerformed(evt);
            }
        });

        txtNombreEtiqueta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreEtiquetaActionPerformed(evt);
            }
        });

        txtValor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtValorActionPerformed(evt);
            }
        });

        btnAgregarNodo.setText("Agregar");
        btnAgregarNodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarNodoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelDatosNodoLayout = new javax.swing.GroupLayout(jPanelDatosNodo);
        jPanelDatosNodo.setLayout(jPanelDatosNodoLayout);
        jPanelDatosNodoLayout.setHorizontalGroup(
            jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosNodoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtNombreNodo, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                    .addComponent(txtNombreEtiqueta)
                    .addComponent(txtValor))
                .addContainerGap(108, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelDatosNodoLayout.createSequentialGroup()
                .addContainerGap(359, Short.MAX_VALUE)
                .addComponent(btnAgregarNodo)
                .addContainerGap())
        );
        jPanelDatosNodoLayout.setVerticalGroup(
            jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosNodoLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombreNodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombreEtiqueta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDatosNodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAgregarNodo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTElementos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre Etiqueta", "Valor"
            }
        ));
        jTElementos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTElementosMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTElementos);

        btnEliminarFila.setText("Eliminar Fila");
        btnEliminarFila.setEnabled(false);
        btnEliminarFila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarFilaActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanelDatosNodo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminarFila)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(402, Short.MAX_VALUE)
                .addComponent(btnGuardar)
                .addGap(7, 7, 7)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnEliminarFila)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelDatosNodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtNombreNodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreNodoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNombreNodoActionPerformed

private void txtNombreEtiquetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreEtiquetaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNombreEtiquetaActionPerformed

private void txtValorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtValorActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtValorActionPerformed

private void jTElementosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTElementosMouseReleased

        filaSeleciona = jTElementos.getSelectedRow();
        btnEliminarFila.setEnabled(true);
}//GEN-LAST:event_jTElementosMouseReleased

private void btnAgregarNodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarNodoActionPerformed
if (txtNombreEtiqueta.getText().trim().compareTo("") == 0 || txtValor.getText().trim().compareTo("") == 0 || txtNombreNodo.getText().trim().compareTo("") == 0) {

            JOptionPane.showMessageDialog(this, "Los campos de texto no pueden estar vacios", "Atencion", JOptionPane.INFORMATION_MESSAGE);

        }else{

             String eti = txtNombreEtiqueta.getText();
            String valor = txtValor.getText();

            DefaultTableModel temp = (DefaultTableModel) jTElementos.getModel();
            Object nuevo[] = {eti, valor};
            temp.addRow(nuevo);

            this.txtNombreEtiqueta.setText("");
            this.txtValor.setText("");
            txtNombreNodo.setEnabled(false);
        }
}//GEN-LAST:event_btnAgregarNodoActionPerformed

private void btnEliminarFilaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarFilaActionPerformed

        int res = JOptionPane.showConfirmDialog(this, "¿Estás seguro de eliminar la fila?", "Confirmar eliminación", JOptionPane.YES_NO_OPTION);

        if (res == JOptionPane.YES_OPTION) {
            DefaultTableModel modelo = (DefaultTableModel) jTElementos.getModel();
            modelo.removeRow(filaSeleciona);
            filaSeleciona = -1;
        }
        btnEliminarFila.setEnabled(false);
}//GEN-LAST:event_btnEliminarFilaActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
this.setVisible(false);
}//GEN-LAST:event_btnCancelarActionPerformed

private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
    String n = txtNombreNodo.getText();

        if (n.equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un nombre de Nodo", "Atencion", JOptionPane.INFORMATION_MESSAGE);
        } else {
            DefaultMutableTreeNode nodo = new DefaultMutableTreeNode(n);
            DefaultTableModel m = (DefaultTableModel) jTElementos.getModel();

            for (int i = 0; i < m.getRowCount(); i++) {
                DefaultMutableTreeNode hijos = new DefaultMutableTreeNode(m.getValueAt(i, 0) + " - " + m.getValueAt(i, 1));
                nodo.add(hijos);

            }
            DefaultTreeModel modelo = (DefaultTreeModel) va.getArbol().getModel();
            DefaultMutableTreeNode cabeza = (DefaultMutableTreeNode) modelo.getRoot();
            cabeza.add(nodo);
            modelo = new DefaultTreeModel(cabeza);
            va.getArbol().setModel(modelo);
            this.setVisible(false);
        }
}//GEN-LAST:event_btnGuardarActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarNodo;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminarFila;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelDatosNodo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTElementos;
    private javax.swing.JTextField txtNombreEtiqueta;
    private javax.swing.JTextField txtNombreNodo;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables
}
